package cn.kgc.vue.service.impl;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.CoursePageDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.service.CourseService;
import cn.kgc.vue.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;


@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService{

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public ResponseResult getPage(CoursePageDTO coursePageDTO) {

        ResponseResult responseResult = new ResponseResult();
        Page<Course> coursePage = new Page<>(coursePageDTO.getCurrentPage(), coursePageDTO.getLimit());
        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        lambda.eq(StringUtils.isNotBlank(coursePageDTO.getCourseName()),Course::getCourseName,coursePageDTO.getCourseName())
                .orderByDesc(Course::getId);

        courseMapper.selectPage(coursePage,lambda);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",coursePage.getTotal());
        map.put("currentPageData",coursePage.getRecords());

        responseResult.success(ResponseStatus.SUCCESS,map);

        return responseResult;
    }
}




